# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# Based on the ebuild by 'mascanho' @ forums.gentoo.org

inherit eutils flag-o-matic wxwidgets

S=${WORKDIR}/${PN}


# Always use todays snapshot by default
DATE=$(date +%Y%m%d)
# Uncomment and edit this line to use a specific date
# Format is <Year><Month><Day>
# DATE=20050402


DESCRIPTION="aMule, the all-platform eMule p2p client"
HOMEPAGE="http://www.amule.org/"
SRC_URI="http://www.hirnriss.net/files/cvs/aMule-CVS-${DATE}.tar.bz2"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86 ~amd64"
IUSE="amuled debug gtk2 kad nls nosystray optimize remote remote-gui stats unicode X"

RESTRICT="nostrip nomirror"

DEPEND="
        gtk2? ( >=x11-libs/wxGTK-2.6.0 )
        amuled? ( >=x11-libs/wxGTK-2.6.0 )
        !gtk2? ( !amuled? ( >=x11-libs/wxGTK-2.6.0 ) )
        >=sys-libs/zlib-1.2.1
        stats? ( >=media-libs/gd-2.0.26 )
        remote? ( >=media-libs/libpng-1.2.0 )
        !net-p2p/amule
        !net-p2p/xmule"

pkg_setup() {
        export WX_GTK_VER="2.6"

        if ! use gtk2 >& /dev/null ; then
                need-wxwidgets gtk || die "gtk version of x11-libs/wxGTK not found"

        elif use unicode >& /dev/null ; then
                need-wxwidgets unicode || die "You need to emerge unicoded wxGTK with USE='gtk2 unicode'"

        else
                need-wxwidgets gtk2 || die "You need to emerge wxGTK with USE='gtk2'"
        fi

        if ! use X && use amuleremotegui; then
                die "You cannot set amuleremotegui while X is not set in your USE flags."
        fi
}

src_compile() {
        if ! use X; then
                MONOLITH="--disable-monolithic"
        fi

        if use kad ?> /dev/null ; then
                KAD="--enable-kad-compile"
        fi

        if use amuled >& /dev/null ; then
                AMULED="--enable-amule-daemon --enable-amule-gui"
        fi

        if use nosystray >& /dev/null ; then
                SYSTRAY="--disable-systray"
        elif use unicode >& /dev/null ; then
                SYSTRAY="--enable-utf8-systray"
        fi

        if use optimize >& /dev/null ; then
                OPTIMIZE="--enable-optimize"
        fi

        if use debug >& /dev/null ; then
                DEBUG="--enable-debug"
        fi

        econf \
                --with-wx-config=${WX_CONFIG} \
                --with-wxbase-config=${WX_CONFIG} \
                ${DEBUG} \
                ${AMULED} \
                ${MONOLITH} \
                ${OPTIMIZE} \
                ${SYSTRAY} \
                ${KAD} \
                `use_enable remote-gui amule-gui` \
                `use_enable nls` \
                `use_enable remote amulecmd` \
                `use_enable remote webserver` \
                `use_enable stats cas` \
                `use_enable stats wxcas` \
                `use_enable remote amulecmdgui` \
                `use_enable remote webservergui` \
                `use_enable stats alc` \
                `use_enable stats alcc` || die

        # we filter ssp until bug #74457 is closed to build on hardened
        if has_hardened; then
        filter-flags -fstack-protector -fstack-protector-all
        fi
        emake -j1 || die
}

src_install() {
        make DESTDIR=${D} install || die

        if use amuled || use remote; then
                if ! id p2p >/dev/null; then
                        enewgroup p2p
                        enewuser p2p -1 /bin/false /home/p2p p2p
                fi
        fi

        if use amuled; then
                insinto /etc/conf.d; newins ${FILESDIR}/amuled.confd amuled
                exeinto /etc/init.d; newexe ${FILESDIR}/amuled.initd amuled
        fi

        if use remote; then
                insinto /etc/conf.d; newins ${FILESDIR}/amuleweb.confd amuleweb
                exeinto /etc/init.d; newexe ${FILESDIR}/amuleweb.initd amuleweb
        fi
}